var form;
var table;
var layer;
layui.use(['layer','form','table'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    //加载优惠券信息
    selCoupon();
    //修改优惠券状态
    form.on('switch', function(data){
        var flag=data.elem.checked;
        //获取优惠券状态
        var online=null;
        if(flag){
            online=2;
        }else{
            online=3;
        }
        //优惠券id
        var coupon_id=data.elem.value;
        $.ajax({
            url: "/SyOperationController/updCouponOnline",
            dataType: "json",
            type:"post",
            data:{"online":online,"coupon_id":coupon_id},
            success: function (data) {
                if(data.code==0){
                    layer.msg('修改成功');
                }else{
                    layer.msg('修改失败');
                }
            }
        });
    });
});
//刷新
function refresh() {
    location.reload();
}
//返回
function go() {
    history.go(-1)
}
//优惠券数据
function selCoupon(){
    //优惠券名称
    var couponName=$("#couponName").val();
    //发放方式
    var mode=$("#mode").val();
    //优惠券状态
    var online=$("#online").val();
    //开始时间
    var startDate=$("#startDate").val();
    //结束时间
    var endDate=$("#endDate").val();
    alert(couponName+mode+online+startDate+endDate);
    alert(1)
    $.ajax({
        url: "/SyOperationController/selCarousel",
        type: "post",
        dataType: "json",
        data:{"couponName":couponName,"mode":mode,"online":online,"startDate":startDate,"endDate":endDate},
        success: function (data) {
            getTable(data.obj);
        }
    });
}
//优惠券数据列表
function getTable(data){
    var tb1=table.render({
        elem:"#table_id",
        title:"优惠券数据列表",
        limit:5,
        data:data,
        page:true,
        cols:[[
            {type:'checkbox',width:60},
            {field:'carouselId',title:'编号',width:100,align:'center'},
            {field:'createUser',title:'创建人',width:150,align:'center'},
            {field:'startDate',title:'开始时间',width:200,align:'center',templet:function(d){
                    var date=d.start_date;
                    date = date.slice(0, date.indexOf("."));
                    date = date.replaceAll('T',' ');
                    return date;
                }},
            {field:'expireDate',title:'结束时间',width:200,align:'center',templet:function(d){
                    var date=d.end_date;
                    date = date.slice(0, date.indexOf("."));
                    date = date.replaceAll('T',' ');
                    return date;
                }},
            {field:'online',title:'上架/下架',width:150,align:'center',templet:function(d){
                    if(d.online==2){
                        return  "<input type='checkbox' checked name='opan' value=\""+d.coupon_id+"\" lay-skin='switch'  >"
                    }else if(d.online==3){
                        return  "<input type='checkbox' name='close' lay-skin='switch' value=\""+d.coupon_id+"\">"
                    }
                }},
            {field:'mode',title:'发放方式',width:150,align:'center',templet:function(d){
                    if(d.mode==2){
                        return  "<div>自动发放</div>"
                    }else if(d.mode==3){
                        return  "<div>活动领取</div>"
                    }else if(d.mode==4){
                        return  "<div>人工发放</div>"
                    }else if(d.mode==5){
                        return  "<div>无限制</div>"
                    }
                }},
            {field:'surplus',title:'数量',width:120,align:'center'},
            {field:'',title:'操作',width:150,align:'center',templet:function(d){
                    return "<span ><a  onclick='javascript:selCoupon2(\""+d.coupon_id+"\")' style='color:#1ABC9C;'>查看</a></span>"+
                        "<span style='margin-left: 20px'><a  onclick='javascript:delCoupon(\""+d.coupon_id+"\")' style='color:#1ABC9C;'>删除</a></span>"
                }}
        ]]
    });
}
//点击查检索页面
function retrieve() {
    //点击检索之后清空输入框
    $("input").val("");
    type: "post",
        $(this).find("option:selected").removeAttr("selected");
    layer.open({
        type : 2,//弹出层的类型
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>样品分类</div>",
        area : [ '670px', '500px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponAdvancedSearch.html",
        success:function(){
        }
    });
    window.layer_1=layer;
}
//点击查看按钮弹出详情页面
function selCoupon2(couponId){
    $.ajax({
        url: "/SyOperationController/selCoupon",
        dataType: "json",
        type:"post",
        data:{"couponId":couponId},
        success: function (data) {
            getTable2(data.obj[0])
        }
    })
}
function getTable2(data){
    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>优惠券详情</div>",
        area : [ '670px', '450px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CouponDetails.html",
        success:function(layero,index){
            //处理开始时间
            var startDate=data.start_date;
            startDate = startDate.slice(0, startDate.indexOf("."));
            startDate = startDate.replaceAll('T',' ');
            //处理结束时间
            var endDate=data.end_date;
            endDate = endDate.slice(0, endDate.indexOf("."));
            endDate = endDate.replaceAll('T',' ');
            //处理优惠券状态
            var online=null;
            if(data.online==2){
                online="上线";
            }else if(data.online==3){
                online="不上线";
            }
            //获取商品状态
            var sate=null;
            if(data.product_sate==0){
                sate="删除";
            }else if(data.product_sate==1){
                sate="已上架";
            }else if(data.product_sate==2){
                sate="审核中"
            }
            //处理获取方式
            var mode=null;
            if(data.mode==2){
                mode="自动发放"
            }else if(data.mode==3){
                mode="活动领取"
            }else if(data.mode==4){
                mode="人工发放"
            }else if(data.mode==5){
                mode="无限制"
            }
            var obj=layero.find('iframe').contents();
            obj.find('input[name=couponCode]').val(data.coupon_code);
            obj.find('input[name=couponName]').val(data.coupon_name);
            obj.find('input[name=startDate]').val(startDate);
            obj.find('input[name=endDate]').val(endDate);
            obj.find('input[name=online]').val(online);
            obj.find('input[name=mode]').val(mode);
            obj.find('input[name=surplus]').val(data.surplus+" 张");
            obj.find('#remark').val(data.remark);
        }
    });
    window.layer_1=layer;
}

//删除优惠券
function delCoupon(couponId) {
    $.ajax({
        url: "/SyOperationController/delCoupon",
        dataType: "json",
        type:"post",
        data:{"couponId":couponId},
        success: function (data) {
            if(data.code==0){
                layer.msg('删除成功');
                selCoupon();
            }
        }
    });
}

//添加页面
function addCoupon() {
    location.href="CouponAdded.html?";
}